package com.zys.el.user.util;

import com.zys.el.user.util.time.MyGa;
import com.zys.el.user.util.time.Schedule;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TempTimesUtils {


    public List<List<String>> TimeList() {
        List<List<String>> re = new ArrayList<>();
        String[][] arr = new String[6][134];
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 134; j++) {
                arr[i][j] = "";
            }
        }
        MyGa ga = new MyGa();
        ArrayList<String> objects = new ArrayList<>();
        objects.add("时间");
        objects.addAll(generateTimeList("08:00", "11:55"));
        objects.addAll(generateTimeList("13:00", "17:55"));
        objects.addAll(generateTimeList("19:00", "21:00"));
        re.add(objects);
        List<List<Schedule>> result = ga.result();
//        System.out.println(result);

        for (int k = 1; k <= 5; k++) {
            List<Schedule> schedules = result.get(k); // 得到一天的
            arr[k][0] = "星期" + numberToChinese(k);
            for (Schedule schedule : schedules) {
                for (Integer s : schedule.getSlot()) {
                    arr[k][s + 1] = "1";
                }
            }
            List<String> strings = Arrays.asList(arr[k]);
            re.add(strings);

        }

        return re;

    }


    // 传入两个"08:00"  "21:00"就可以获得列表
    public static List<String> generateTimeList(String startTime, String endTime) {
        LocalTime start = LocalTime.parse(startTime, DateTimeFormatter.ofPattern("HH:mm"));
        LocalTime end = LocalTime.parse(endTime, DateTimeFormatter.ofPattern("HH:mm"));
        List<String> timeList = new ArrayList<>();

        while (!start.isAfter(end)) {
            timeList.add(start.format(DateTimeFormatter.ofPattern("HH:mm")));
            start = start.plusMinutes(5);
        }

        return timeList;
    }

    public static String numberToChinese(int number) {
        if (number == 0) {
            return "零";
        }

        String[] units = {"", "十", "百", "千", "万", "十万", "百万", "千万", "亿"};
        String[] digits = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};

        StringBuilder sb = new StringBuilder();
        int unitIndex = 0;

        while (number > 0) {
            int digit = number % 10;
            if (digit > 0) {
                sb.insert(0, digits[digit] + units[unitIndex]);
            } else {
                // 处理连续的零
                if (sb.length() > 0 && !sb.substring(0, 1).equals("零")) {
                    sb.insert(0, "零");
                }
            }

            unitIndex++;
            number = number / 10;
        }

        return sb.toString();
    }

}
